package com.guli.coupon.service.impl;

import com.guli.coupon.entity.SeckillSkuRelationEntity;
import com.guli.coupon.service.SeckillSkuRelationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.jni.Local;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.coupon.dao.SeckillSessionDao;
import com.guli.coupon.entity.SeckillSessionEntity;
import com.guli.coupon.service.SeckillSessionService;


@Service("seckillSessionService")
@Slf4j
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {
    @Autowired
    SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> getLatest3DaySession() {
        List<SeckillSessionEntity> sessionEntities = this.list
                (new QueryWrapper<SeckillSessionEntity>()
                        .between("start_time",
                                this.startTime(),
                                this.endTime()));
        if (sessionEntities.size() <= 0) {
            log.info("{} => 三天后暂无活动场次安排", LocalDateTime.now());
            return null;
        }
        List<SeckillSessionEntity> seckillSessionEntities = sessionEntities
                .stream().peek(session -> {
                    Long id = session.getId();
                    List<SeckillSkuRelationEntity> relationEntities = seckillSkuRelationService
                            .list(new QueryWrapper<SeckillSkuRelationEntity>()
                                    .eq("promotion_session_id", id));
                    session.setRelationEntities(relationEntities);
                }).collect(Collectors.toList());
        return seckillSessionEntities;
    }

    private String startTime() {
        LocalDate now = LocalDate.now();
        LocalTime min = LocalTime.MIN;
        LocalDateTime startTime = LocalDateTime.of(now, min);
        String format = startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }

    private String endTime() {
        LocalDate now = LocalDate.now();
        LocalDate plusDays = now.plusDays(3);
        LocalTime max = LocalTime.MAX;
        LocalDateTime endTime = LocalDateTime.of(plusDays, max);
        String end = endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return end;
    }

}